<?php
namespace Home\Controller;
use Think\Controller;
class PrepaidCardController extends CommonController {
    public function data_list(){
        $limit = I("pageSize", 30, "intval");
        $page = I("pageCurrent", 1, "intval");
        $search_words = I('search_words');
        $condition['pc_number'] = array('like','%'.$search_words.'%');
        $this->assign('search_words',$search_words);

        if(I('us_id')!=null&&I('us_id')!=''){
            $condition['pc_use_user'] = I('us_id');
            $this->assign("us_id",I('us_id'));
        }
        $prepaidCard_model = D("PrepaidCard");
        $count = $prepaidCard_model->get_count($condition);
        $dataList = $prepaidCard_model->get_prepaidCard_list($condition, $limit, $page);
        $this->assign("count",$count);
        $this->assign("dataList",$dataList);
    	$this->display();
    }
    public function operation(){
        if(IS_POST){
            $num = (int)$_POST['num'];
            $pc_value=$_POST['pc_value'];
            $prepaidCard_model = D("PrepaidCard");

            $i = 0;
            while($i<$num) {
                $randChar = getRandChar(11);
                $find = $prepaidCard_model->where(array('pc_number'=>$randChar))->find();
                if(empty($find)){
                    $data = array(
                        'pc_number'=>$randChar,
                        'pc_value'=>$pc_value,
                        'pc_create_time'=>time(),
                    );
                    $result = $prepaidCard_model->add($data);
                    if($result){
                       $i++; 
                    }
                }
            }
            exit(success("成功生成".($i).'张 面值：'.$pc_value."的充值卡！",true));
        }else{
            $this->display();
        }
    }
}